package hai.com.android_test.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import hai.com.android_test.R;
import hai.com.android_test.base.BaseActivity;

/**
 * Created by 黄海 on 2017/1/9.
 */

public class ViewPageActivity extends BaseActivity {
    private static final String TAG = "hhp";
    @BindView(R.id.viewPage)
    ViewPager viewPage;
    @BindView(R.id.indicatorContainer)
    LinearLayout indicatorContainer;
    Integer[] ids = new Integer[]{R.drawable.withdraw0, R.drawable.withdraw1, R.drawable.withdraw2, R.drawable.withdraw3};
    private List<View> views = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpage);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        for (int i = 0; i < ids.length; i++) {
            ImageView image = new ImageView(ViewPageActivity.this);
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            image.setImageResource(ids[i]);
            views.add(image);

            View indi = LayoutInflater.from(this).inflate(R.layout.item_viewpage_indicator, null);
            indi.setSelected(false);
            indicatorContainer.addView(indi);
        }
        viewPage.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return ids.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                Log.d(TAG, "instantiateItem() called with: container = [" + container + "], position = [" + position + "]");
                container.addView(views.get(position));
                return views.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                Log.d(TAG, "destroyItem() called with: container = [" + container + "], position = [" + position + "], object = [" + object + "]");
                container.removeView(views.get(position));
            }
        });
    }
}
